package cn.szzsi._webpc;

import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.IdGen;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Message;
import cn.szzsi.common.model.SiteConfig;

import cn.szzsi.service.UserBalanceService;
import cn.szzsi.service.UserPointDetailService;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import java.math.BigDecimal;


/**
 * 前台类 - 用户中心
 * Created by haotian on 2016/10/14.
 */
public class CustomerCenterController extends BaseWebPcController<Member> {
    private String path;

    public void index() {//用户中心
        Member member = getAttr("loginMember");
        setAttr("loginMember", member);
        //查看该会员今天有没有签到
        if (UserPointDetailService.service.checkIfUserSign(member)) {
            setAttr("alreadySign", "alreadySign");
        }
        setAttr("totalProfit", UserBalanceService.service.getBalance(member));
        pcTemplatePath("fansindex.html");
        //getTemplatePath(member.getShopId(), "fansindex");

    }


    //绑定手机号码
    @Clear
    public void phoneapply() {
        Member member = getLoginMember();
        String shopId = getPara("shopId");
        path = CommonUtil.getPathByUidAndType(shopId, CommonUtil.isMobileDevice(getRequest()));//获取模板路径
        setAttr("loginMember", member);
        setAttr("base", getRequest().getContextPath());
        getTemplatePath(shopId, "phone_apply");
    }

    //发送手机号码
    @Clear
    public void sendSms() {
        String phone = getPara("phone");
        String shopId = getPara("shopId");
    }

    //手机验证码
    @Clear
    public void captcha() {
        renderJson(Msg.SUCCESS);
    }

    //绑定手机号码保存
    @Clear
    public void phoneapplysave() {
        Member member = getLoginMember();
        String phoone = getPara("phone");
        String realname = getPara("realname");
        String idno = getPara("idno");
        String captcha = getPara("captcha");
        if (getSessionAttr("SMS_KEY") == null) {
            renderJson(Msg.fail("手机验证码不正确", null));
            return;
        }
        if (!captcha.equalsIgnoreCase(getSessionAttr("SMS_KEY").toString())) {
            renderJson(Msg.fail("手机验证码不正确", null));
        } else {
            member.set("phone", phoone);
            member.set("realname", realname);
            member.set("idno", idno);
            member.update();
            renderJson(Msg.SUCCESS);
        }
    }

    //我的钱包
    @Clear
    public void capital() {
        String shopId = getPara("shopId");
        path = CommonUtil.getPathByUidAndType(shopId, CommonUtil.isMobileDevice(getRequest()));//获取模板路径
        Member member = getLoginMember();
        setAttr("m", member);
        if (member != null) {
            //根据用户类型来判断 head profit
            //分销金额
            if (member.getStr("memberRankId") != null && member.getStr("memberRankId").equals("sf60982c5faf48cb8f59a492a73a6b2c")) {//准会员(金额全部冻结)
                setAttr("freezeMoney", UserBalanceService.service.getFreezeMoney(member));
                setAttr("availBalance", 0);
                setAttr("totalProfit", 0);
            } else {
                BigDecimal totalProfit = UserBalanceService.service.getBalance(member);//总余额
                BigDecimal freezeMoney = UserBalanceService.service.getFreezeMoney(member);//冻结金额
                BigDecimal availBalance = totalProfit.subtract(freezeMoney);
                if (availBalance.compareTo(new BigDecimal(0)) < 0) {
                    availBalance = new BigDecimal(0);
                }
                setAttr("totalProfit", totalProfit);
                setAttr("freezeMoney", freezeMoney);
                setAttr("availBalance", availBalance);//可用金额(总余额-冻结金额)
            }

        }
        getTemplatePath(shopId, "capital");


    }

    // 获取当前登录会员，若未登录则返回null
    public Member getLoginMember() {
        String id = getSessionAttr(Member.LOGIN_MEMBER_ID_SESSION_NAME);
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        Member loginMember = Member.dao.findById(id);
        return loginMember;
    }

    //用户注销
    //@Clear
    public void logout() {
        //清除用户信息
        getRequest().getSession().removeAttribute(Member.LOGIN_MEMBER_ID_SESSION_NAME);
        Cookie loginMemberUsernameCookie = new Cookie(Member.LOGIN_MEMBER_USERNAME_COOKIE_NAME, null);
        Cookie loginTypeCookie = new Cookie(Member.LOGIN_TYPE_COOKIE_NAME, null);
        loginMemberUsernameCookie.setPath(getRequest().getContextPath() + "/");
        loginTypeCookie.setPath(getRequest().getContextPath() + "/");
        loginMemberUsernameCookie.setMaxAge(0);
        loginTypeCookie.setMaxAge(0);
        getResponse().addCookie(loginMemberUsernameCookie);
        getResponse().addCookie(loginTypeCookie);
        redirect("/");

    }

}
